/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.model;

import javax.media.opengl.GL3bc;
import jinngine.geometry.Box;
import jinngine.geometry.ConvexHull;
import jinngine.geometry.Geometry;
import jinngine.math.Matrix3;
import jinngine.math.Matrix4;
import jinngine.math.Transforms;
import jinngine.math.Vector3;
import space.gl.RenderContext;

/**
 *
 * @author Pierre Labatut
 */
public class DrawGeometry {

    public static Drawable buildDrawable(final Geometry geom) {
        if (geom instanceof Box) {
            return new Drawable() {

                public Drawable optimize(GL3bc gl) {
                    return this;
                }

                public void draw(final RenderContext rc) {
                    final Matrix3 r = new Matrix3();
                    final Vector3 t = new Vector3();
                    geom.getLocalTransform(r, t);
                    final Matrix4 m = Transforms.transformAndTranslate4(r, t);
                    rc.compose(m);
                    rc.applyLegacyMatrix();
                    final GL3bc gl = rc.gl();
                    gl.glBegin(GL3bc.GL_QUADS);
                    final float d = .5f;
                    for (float s : new float[]{-d, d}) {
                        gl.glVertex3f(-d, -d, s);
                        gl.glVertex3f(+d, -d, s);
                        gl.glVertex3f(+d, +d, s);
                        gl.glVertex3f(-d, +d, s);

                        gl.glVertex3f(-d, s, -d);
                        gl.glVertex3f(+d, s, -d);
                        gl.glVertex3f(+d, s, +d);
                        gl.glVertex3f(-d, s, +d);

                        gl.glVertex3f(s, -d, -d);
                        gl.glVertex3f(s, +d, -d);
                        gl.glVertex3f(s, +d, +d);
                        gl.glVertex3f(s, -d, +d);
                    }
                    gl.glEnd();                    
                }

                public void dispose(GL3bc gl) {
                }
            };
        } else if (geom instanceof ConvexHull) {
            return new Drawable() {

                public Drawable optimize(GL3bc gl) {
                    return this;
                }

                public void draw(RenderContext rc) {
                    final GL3bc gl = rc.gl();
                    gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE);
                    gl.glColor3f(.8f, .2f, .2f);
                    rc.setObjectWorld(geom.getWorldTransform());
                    rc.applyLegacyMatrix();
                    rc.stopUsingProgram();
                    for (final Vector3[] vects : ((ConvexHull) geom).getFaces()) {
                        gl.glBegin(GL3bc.GL_POLYGON);
                        for (Vector3 v : vects) {
                            gl.glVertex3d(v.x, v.y, v.z);
                        }
                        gl.glEnd();
                    }
                    gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL);
                    rc.startUsingProgram();
                }

                public void dispose(GL3bc gl) {
                }
            };

        } else {
            throw new RuntimeException();
        }
    }
}
